//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/12.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit

// 协议
@objc protocol visitorLoginViewDelegate: NSObjectProtocol {
    // 声明协议方法
    // 用户登录  optional 可选方法
    optional func userWillLogin()
    // 用户注册
    func userWillRegister()
}

class VisitorLoginView: UIView {

    // 声明代理   需要使用weak
    weak var visitorViewDelegate: visitorLoginViewDelegate?
    
    // 设置对外提供访客信息的方法
    func setupInfo(tipText: String, imageName: String?) {
        
        tipLabel.text = tipText
        
        if let name = imageName { // 如果有图片设置
            // 这里是其他页面的信息
            circleView.image = UIImage(named: name)
            // 隐藏大房子的图片
            largeIcon.hidden = true
            // 把图片移到最上面
            bringSubviewToFront(circleView)
//            backView.hidden = true
          
        } else {
            // 这里是首页的信息
            // 开始做圆形图片转动的动画
            startAnimation()
        }
    }
    
    // 运行图片转动动画
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画属性
        animation.repeatCount = MAXFLOAT
        animation.duration = 10.0
        animation.toValue = 2 * M_PI
        // 当动画完成或者页面失去活跃状态  动画不移除图层
        animation.removedOnCompletion = false
        // 添加动画
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // 设置所有的子视图
    private func setupUI() {
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeIcon)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        
        // 使用手码添加约束需要屏蔽frame布局
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        //添加圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置背景视图的约束 可视化的布局语言
        //OC 中可选项 一般使用 按位 或枚举选项  swift中直接指定数组
        //metrics:约束数值字典 [String: 数值],
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn":registerBtn]))
        
        // 设置背景颜色  灰度
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        // 添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    // MARK:添加登陆和注册按钮的监听事件
    @objc private func loginBtnDidClick() {
        
        visitorViewDelegate?.userWillLogin?()
        
    }
    @objc private func registerBtnDidClick() {
        // 如果为nil就不会执行这个方法
        visitorViewDelegate?.userWillRegister()
    }
    
    
    // 懒加载所有的子视图
    private lazy var largeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 本页面信息
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    // 注册按钮
    private lazy var registerBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", fontSize: 15, color: themeColor)
    // 登陆按钮
    private lazy var loginBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "登陆", fontSize: 15, color: UIColor.darkGrayColor())
    
    
}